package com.heima.service.impl;

import com.heima.dao.AccountDao;
import com.heima.pojo.Account;
import com.heima.service.AccountService;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

@Service
public class AccountServiceImpl implements AccountService {
    /*DataSourceTransactionManager transactionManager = null;
    DefaultTransactionDefinition definition = null;
    TransactionStatus transactionStatus = null;*/

    @Autowired
    AccountDao accountDao;

    /*@Autowired
    DataSource dataSource;*/


    @Override
    public void transfer(String fromName, String toName, double money) {
        //获取发起转账的用户
        Account fName = accountDao.findByName(fromName);
        Account tName = accountDao.findByName(toName);
        //进行转账操作
        fName.setMoney(fName.getMoney() - money);
        /*System.out.println(1/0);*/
        tName.setMoney(tName.getMoney() + money);
        //操作完成刷新
        accountDao.update(fName);
        accountDao.update(tName);
    }

    /*@Before
    public void before(){
        //传入数据源参数
        transactionManager = new DataSourceTransactionManager(dataSource);
        //定义事务的具体信息
        definition = new DefaultTransactionDefinition();
        //开启事务，返回事务的状态
        transactionStatus = transactionManager.getTransaction(definition);
    }*/
    /*@After
    public void after(){
        //提交事务
        transactionManager.commit(transactionStatus);
    }*/

}
